package Test;

import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class TextGenerater extends GUI{
	public String lastletter;
	public String nextletter;
	public String text="";
	public char firstletter;
	private HashMap<String, Integer> analysis;
	private String secondlastletter;
	private String thirdlastletter;
	private String fourthlastletter;
	private String fifthlastletter;
	private String sixlastletter;
	private String t;
	public File inputFile;
	
	static int windowSize;
	static String textlaenge;
	static int wertlang;

	
	public TextGenerater(String total){
		
		textlaenge=total;
		
		wertlang=total.length();
		
	}
	
	public TextGenerater(int groesse, File InputFile){
	windowSize=groesse;
	inputFile=InputFile;
	}
	public TextGenerater(char firstletter){
	
		this.firstletter = firstletter;
		text=String.valueOf(firstletter);

		
		
	}
//	 progressBar.paint(progressBar.getGraphics());

	public void gen2(HashMap<String, Integer> analysis){
		
		this.analysis = analysis;		
		generate();
	}
	
	
	public void generate(){
		
		// start working
		          lastletter = String.valueOf(firstletter);	
		
		         SimpleAnalysis.mainProg.m2(wertlang);

				 //run through till reach the length of input text
		          
		for(int ti=0;ti<wertlang;ti++) {
			// for progress bar		
			if(ti%5==0){
			  SimpleAnalysis.mainProg.setProgress(ti);
			}
				  	
				    Set<String> keySet = this.analysis.keySet();
				    Iterator<String> i = keySet.iterator();
				    
				    List<Integer> wert = new ArrayList<Integer>();
				    List<String> letter = new ArrayList<String>();
				    letter.clear();
				    wert.clear();
				    
			if(windowSize==1){
				
				while(i.hasNext()){
					String key =i.next();
					letter.add(key);
					wert.add(analysis.get(key));
				}
			}
				    
			//check if key has  2 letters
						
		    if(windowSize==2){
		    	
		    	while(i.hasNext()){	
				
						String key = i.next();
						String check = key.substring(0, 1);
						
						
						
					
						if(lastletter.equals(check)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(1,2));
							wert.add(analysis.get(key));
								
								}	
							}
		    			}
		    
		    // check if key has 3 letters
		    
		    
		    
		    if(windowSize==3){
		    	
	
		    	
						    // if it is the first time you only have 2 key to analyze	
						    	if(ti==0){
							    	while(i.hasNext()){	
										
												String key = i.next();
												String check = key.substring(0,1);
											
											
												if(lastletter.equals(check)){
													
														//Werte im arry abspeichern
												    
													letter.add(key.substring(1,2));
													wert.add(analysis.get(key));
														
														}	
													}
						    		
						    	 			}
						    	else{
						    		
						    		while(i.hasNext()){	
									
										String key = i.next();
										String check = key.substring(1,2);
										String check2 = key.substring(0,1);
									
										if(lastletter.equals(check)){
											
											if(secondlastletter.equals(check2)){
											
												//Werte im arry abspeichern
											
											letter.add(key.substring(2,3));
											wert.add(analysis.get(key));
											
												}	
											}
					
						    		}
			    		if(wert.isEmpty()){
			    			Set<String> keySet2 = this.analysis.keySet();
						    Iterator<String> i2 = keySet2.iterator();
			    			
			    			while(i2.hasNext()){	
								
								String key = i2.next();
								String neucheck = key.substring(0,1);
							
								if(lastletter.equals(neucheck)){
									
										//Werte im arry abspeichern
									System.out.println("empty");
									letter.add(key.substring(1,2));
									wert.add(analysis.get(key));
										
										}	
									}
		    		
			    				}	
					}
		    }
						    	
				if(windowSize==4){
				System.out.println("yes");	
				//windowSize4();
					// if it is the first time you only have 2 key to analyze	
				
			    	if(ti==0){
				    	while(i.hasNext()){	
							
									String key = i.next();
									String check = key.substring(0,1);
								
								
									if(lastletter.equals(check)){
										
											//Werte im array abspeichern
									    
										letter.add(key.substring(1,2));
										wert.add(analysis.get(key));
											
											}	
										}
			    		
			    	 			}
			    	if(ti==1){
			    		
			    		while(i.hasNext()){	
						
							String key = i.next();
							String check = key.substring(1,2);
							String check2 = key.substring(0,1);
						
							if(lastletter.equals(check)){
								
								if(secondlastletter.equals(check2)){
								
									//Werte im arry abspeichern
								
								letter.add(key.substring(2,3));
								wert.add(analysis.get(key));
								
									}	
								}
		
			    		}
			    	}
			    else{
			    	
			    	while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(2,3);
						String check2 = key.substring(1,2);
						String check3 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(3,4));
							wert.add(analysis.get(key));
							
								}	
							}
	
		    	        	}
						}
			    	}
			    	
			        if(wert.isEmpty()){
		    			Set<String> keySet3 = this.analysis.keySet();
					    Iterator<String> i3 = keySet3.iterator();
		    			
		    			while(i3.hasNext()){	
							
							String key = i3.next();
							String neucheck = key.substring(1,2);
							String neucheck1 = key.substring(0,1);
						
							if(lastletter.equals(neucheck)){
								if(secondlastletter.equals(neucheck1)){
								
									//Werte im arry abspeichern
								System.out.println("empty 2");
								letter.add(key.substring(2,3));
								wert.add(analysis.get(key));
								}
									}	
								}
				
		    				}	
			    	
			    	
    		if(wert.isEmpty()){
    			Set<String> keySet2 = this.analysis.keySet();
			    Iterator<String> i2 = keySet2.iterator();
    			
    			while(i2.hasNext()){	
					
					String key = i2.next();
					String neucheck = key.substring(0,1);
				
					if(lastletter.equals(neucheck)){
						
							//Werte im array abspeichern
						System.out.println("empty");
						letter.add(key.substring(1,2));
						wert.add(analysis.get(key));
							
							}	
						}
		
    				}	
    		}
			
				
				if(windowSize==5){
					if(ti==0){
				    	while(i.hasNext()){	
							
									String key = i.next();
									String check = key.substring(0,1);
								
								
									if(lastletter.equals(check)){
										
											//Werte im array abspeichern
									    
										letter.add(key.substring(1,2));
										wert.add(analysis.get(key));
											
											}	
										}
			    		
			    	 			}
			    	if(ti==1){
			    		
			    		while(i.hasNext()){	
						
							String key = i.next();
							String check = key.substring(1,2);
							String check2 = key.substring(0,1);
						
							if(lastletter.equals(check)){
								
								if(secondlastletter.equals(check2)){
								
									//Werte im arry abspeichern
								
								letter.add(key.substring(2,3));
								wert.add(analysis.get(key));
								
									}	
								}
		
			    		}
			    	}
			    if(ti==2){
			    	
			    	while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(2,3);
						String check2 = key.substring(1,2);
						String check3 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(3,4));
							wert.add(analysis.get(key));
							
								}	
							}
	
		    	        	}
						}
			    	}
			    else{
	while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(3,4);
						String check2 = key.substring(2,3);
						String check3 = key.substring(1,2);
						String check4 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
									
									if(fourthlastletter.equals(check4)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(4,5));
							wert.add(analysis.get(key));
							
									}
								}	
							}
	
		    	        	}
						}
			    }
			    
			    if(wert.isEmpty()){
			    	Set<String> keySet4 = this.analysis.keySet();
			        Iterator<String> i4 = keySet4.iterator();
			    	System.out.println("nope");
			    	while(i4.hasNext()){	
			    		
			    		String key = i4.next();
			    		String neucheck = key.substring(2,3);
			    		String neucheck1 = key.substring(1,2);
			    		String neucheck2 = key.substring(0,1);
			    	
			    		if(lastletter.equals(neucheck)){
			    			if(secondlastletter.equals(neucheck1)){
			    				if(thirdlastletter.equals(neucheck2)){
			    			
			    				//Werte im arry abspeichern
			    			System.out.println("empty 3");
			    			letter.add(key.substring(3,4));
			    			wert.add(analysis.get(key));
			    			}
			    				}	
			    			}

			    		}	
			    }
			    
			    if(wert.isEmpty()){
	    			Set<String> keySet3 = this.analysis.keySet();
				    Iterator<String> i3 = keySet3.iterator();
	    			
	    			while(i3.hasNext()){	
						
						String key = i3.next();
						String neucheck = key.substring(1,2);
						String neucheck1 = key.substring(0,1);
					
						if(lastletter.equals(neucheck)){
							if(secondlastletter.equals(neucheck1)){
							
								//Werte im arry abspeichern
							System.out.println("empty 2");
							letter.add(key.substring(2,3));
							wert.add(analysis.get(key));
							}
								}	
							}
			
	    				}	
			
    		if(wert.isEmpty()){
    			Set<String> keySet2 = this.analysis.keySet();
			    Iterator<String> i2 = keySet2.iterator();
    			
    			while(i2.hasNext()){	
					
					String key = i2.next();
					String neucheck = key.substring(0,1);
				
					if(lastletter.equals(neucheck)){
						
							//Werte im arry abspeichern
						System.out.println("emptynnn");
						letter.add(key.substring(1,2));
						wert.add(analysis.get(key));
							
							}	
						}
		
    				}	
				}
				
				if(windowSize==6){
					
					if(ti==0){
			    	while(i.hasNext()){	
						
								String key = i.next();
								String check = key.substring(0,1);
							
							
								if(lastletter.equals(check)){
									
										//Werte im array abspeichern
								    
									letter.add(key.substring(1,2));
									wert.add(analysis.get(key));
										
										}	
									}
		    		
		    	 			}
		    	if(ti==1){
		    		
		    		while(i.hasNext()){	
					
						String key = i.next();
						String check = key.substring(1,2);
						String check2 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
							
								//Werte im arry abspeichern
							
							letter.add(key.substring(2,3));
							wert.add(analysis.get(key));
							
								}	
							}
	
		    		}
		    	}
		    if(ti==2){
		    	
		    	while(i.hasNext()){	
					
					String key = i.next();
					String check = key.substring(2,3);
					String check2 = key.substring(1,2);
					String check3 = key.substring(0,1);
				
					if(lastletter.equals(check)){
						
						if(secondlastletter.equals(check2)){
							
							if(thirdlastletter.equals(check3)){
							
							//Werte im arry abspeichern
						
						letter.add(key.substring(3,4));
						wert.add(analysis.get(key));
						
							}	
						}

	    	        	}
					}
		    	}
		    if(ti==3){
		    	while(i.hasNext()){	
					
					String key = i.next();
					String check = key.substring(3,4);
					String check2 = key.substring(2,3);
					String check3 = key.substring(1,2);
					String check4 = key.substring(0,1);
				
					if(lastletter.equals(check)){
						
						if(secondlastletter.equals(check2)){
							
							if(thirdlastletter.equals(check3)){
								
								if(fourthlastletter.equals(check4)){
							
							//Werte im arry abspeichern
						
						letter.add(key.substring(4,5));
						wert.add(analysis.get(key));
						
								}
							}	
						}

	    	        	}
					}
		    }
		    else{
while(i.hasNext()){	
					
					String key = i.next();
					String check = key.substring(4,5);
					String check2 = key.substring(3,4);
					String check3 = key.substring(2,3);
					String check4 = key.substring(1,2);
					String check5 = key.substring(0,1);
				
					if(lastletter.equals(check)){
						
						if(secondlastletter.equals(check2)){
							
							if(thirdlastletter.equals(check3)){
								
								if(fourthlastletter.equals(check4)){
									
									if(fifthlastletter.equals(check5)){
							
							//Werte im arry abspeichern
						
						letter.add(key.substring(5,6));
						wert.add(analysis.get(key));
						
									}
								}
							}	
						}

	    	        	}
					
					}
System.out.println(wert.isEmpty());

if(wert.isEmpty()){
	Set<String> keySet4 = this.analysis.keySet();
    Iterator<String> i4 = keySet4.iterator();
	System.out.println("nope");
	
	
	while(i4.hasNext()){	
		
		String key = i4.next();
		String neucheck0 = key.substring(3,4);
		String neucheck = key.substring(2,3);
		String neucheck1 = key.substring(1,2);
		String neucheck2 = key.substring(0,1);
	
		if(lastletter.equals(neucheck0)){
			if(secondlastletter.equals(neucheck)){
				if(thirdlastletter.equals(neucheck1)){
					if(fourthlastletter.equals(neucheck2)){
			
				//Werte im array abspeichern
			System.out.println("empty 3");
			letter.add(key.substring(4,5));
			wert.add(analysis.get(key));
						}
					}	
				}

			}	
		}
	}


if(wert.isEmpty()){
	Set<String> keySet4 = this.analysis.keySet();
    Iterator<String> i4 = keySet4.iterator();
	System.out.println("nope");
	while(i4.hasNext()){	
		
		String key = i4.next();
		String neucheck = key.substring(2,3);
		String neucheck1 = key.substring(1,2);
		String neucheck2 = key.substring(0,1);
	
		if(lastletter.equals(neucheck)){
			if(secondlastletter.equals(neucheck1)){
				if(thirdlastletter.equals(neucheck2)){
			
				//Werte im arry abspeichern
			System.out.println("empty 3");
			letter.add(key.substring(3,4));
			wert.add(analysis.get(key));
			}
				}	
			}

		}	
}

               if(wert.isEmpty()){
    			Set<String> keySet3 = this.analysis.keySet();
			    Iterator<String> i3 = keySet3.iterator();
    			
    			while(i3.hasNext()){	
					
					String key = i3.next();
					String neucheck = key.substring(1,2);
					String neucheck1 = key.substring(0,1);
				
					if(lastletter.equals(neucheck)){
						if(secondlastletter.equals(neucheck1)){
						
							//Werte im arry abspeichern
						System.out.println("empty 2");
						letter.add(key.substring(2,3));
						wert.add(analysis.get(key));
						}
							}	
						}
		
    				}	
               if(wert.isEmpty()){
       			Set<String> keySet2 = this.analysis.keySet();
   			    Iterator<String> i2 = keySet2.iterator();
       			
       			while(i2.hasNext()){	
   					
   					String key = i2.next();
   					String neucheck = key.substring(0,1);
   				
   				
   					if(lastletter.equals(neucheck)){
   						
   						
   							//Werte im arry abspeichern
   					//	System.out.println("empty");
   						letter.add(key.substring(1,2));
   						wert.add(analysis.get(key));
   						
   							}	
   						}
   		
       				}	


		    }
		}
		
			
	
					
				if(windowSize==7){		
					if(ti==0){
				    	while(i.hasNext()){	
							
									String key = i.next();
									String check = key.substring(0,1);
								
								
									if(lastletter.equals(check)){
										
											//Werte im array abspeichern
									    
										letter.add(key.substring(1,2));
										wert.add(analysis.get(key));
											
											}	
										}
			    		
			    	 			}
			    	if(ti==1){
			    		
			    		while(i.hasNext()){	
						
							String key = i.next();
							String check = key.substring(1,2);
							String check2 = key.substring(0,1);
						
							if(lastletter.equals(check)){
								
								if(secondlastletter.equals(check2)){
								
									//Werte im arry abspeichern
								
								letter.add(key.substring(2,3));
								wert.add(analysis.get(key));
								
									}	
								}
		
			    		}
			    	}
			    if(ti==2){
			    	
			    	while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(2,3);
						String check2 = key.substring(1,2);
						String check3 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(3,4));
							wert.add(analysis.get(key));
							
								}	
							}

		    	        	}
						}
			    	}
			    if(ti==3){
			    	while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(3,4);
						String check2 = key.substring(2,3);
						String check3 = key.substring(1,2);
						String check4 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
									
									if(fourthlastletter.equals(check4)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(4,5));
							wert.add(analysis.get(key));
							
									}
								}	
							}

		    	        	}
						}
			    }
			    if(ti==4){
	              while(i.hasNext()){	
						
						String key = i.next();
						String check = key.substring(4,5);
						String check2 = key.substring(3,4);
						String check3 = key.substring(2,3);
						String check4 = key.substring(1,2);
						String check5 = key.substring(0,1);
					
						if(lastletter.equals(check)){
							
							if(secondlastletter.equals(check2)){
								
								if(thirdlastletter.equals(check3)){
									
									if(fourthlastletter.equals(check4)){
										
										if(fifthlastletter.equals(check5)){
								
								//Werte im arry abspeichern
							
							letter.add(key.substring(5,6));
							wert.add(analysis.get(key));
							
										}
									}
								}	
							}

		    	       	}
						
					}
			    }
			    else{ 
			    	 while(i.hasNext()){	
							
							String key = i.next();
							String check = key.substring(5,6);
							String check2 = key.substring(4,5);
							String check3 = key.substring(3,4);
							String check4 = key.substring(2,3);
							String check5 = key.substring(1,2);
							String check6 = key.substring(0,1);
						
							if(lastletter.equals(check)){
								
								if(secondlastletter.equals(check2)){
									
									if(thirdlastletter.equals(check3)){
										
										if(fourthlastletter.equals(check4)){
											
											if(fifthlastletter.equals(check5)){
												
												if(sixlastletter.equals(check6)){
									
									//Werte im arry abspeichern
								
								letter.add(key.substring(6,7));
								wert.add(analysis.get(key));
								
											}
										}
									}	
								}

			    	       	}
						}
			    	 }
							
//	System.out.println(wert.isEmpty());
	
	if(wert.isEmpty()){
		Set<String> keySet4 = this.analysis.keySet();
	    Iterator<String> i4 = keySet4.iterator();
		//System.out.println("nope");
		
		
		while(i4.hasNext()){	
			
			String key = i4.next();
			String neucheck00 = key.substring(4,5);
			String neucheck0 = key.substring(3,4);
			String neucheck = key.substring(2,3);
			String neucheck1 = key.substring(1,2);
			String neucheck2 = key.substring(0,1);
		
			if(lastletter.equals(neucheck00)){
				if(secondlastletter.equals(neucheck0)){
					if(thirdlastletter.equals(neucheck)){
						if(fourthlastletter.equals(neucheck1)){
							if(fifthlastletter.equals(neucheck2)){
				
					//Werte im arry abspeichern
				//System.out.println("empty 3");
				letter.add(key.substring(5,6));
				wert.add(analysis.get(key));
							}
						}	
					}

				}
				}	
				
			}
		}
	
	if(wert.isEmpty()){
		Set<String> keySet4 = this.analysis.keySet();
	    Iterator<String> i4 = keySet4.iterator();
		//System.out.println("nope");
		
		
		while(i4.hasNext()){	
			
			String key = i4.next();
			String neucheck0 = key.substring(3,4);
			String neucheck = key.substring(2,3);
			String neucheck1 = key.substring(1,2);
			String neucheck2 = key.substring(0,1);
		
			if(lastletter.equals(neucheck0)){
				if(secondlastletter.equals(neucheck)){
					if(thirdlastletter.equals(neucheck1)){
						if(fourthlastletter.equals(neucheck2)){
				
					//Werte im arry abspeichern
				System.out.println("empty 3");
				letter.add(key.substring(4,5));
				wert.add(analysis.get(key));
							}
						}	
					}

				}	
			}
		}

	               if(wert.isEmpty()){
	    			Set<String> keySet3 = this.analysis.keySet();
				    Iterator<String> i3 = keySet3.iterator();
	    			
	    			while(i3.hasNext()){	
						
						String key = i3.next();
						String neucheck0 = key.substring(2,3);
						String neucheck = key.substring(1,2);
						String neucheck1 = key.substring(0,1);
					
						if(lastletter.equals(neucheck0)){
							if(secondlastletter.equals(neucheck)){
								if(thirdlastletter.equals(neucheck1)){
							
								//Werte im arry abspeichern
							//System.out.println("empty 2");
							letter.add(key.substring(3,4));
							wert.add(analysis.get(key));
							}
								}	
							}
						}
			
	    				}	
	               if(wert.isEmpty()){
		    			Set<String> keySet3 = this.analysis.keySet();
					    Iterator<String> i3 = keySet3.iterator();
		    			
		    			while(i3.hasNext()){	
							
							String key = i3.next();
							String neucheck = key.substring(1,2);
							String neucheck1 = key.substring(0,1);
						
							if(lastletter.equals(neucheck)){
								if(secondlastletter.equals(neucheck1)){
								
									//Werte im arry abspeichern
							//	System.out.println("empty 2");
								letter.add(key.substring(2,3));
								wert.add(analysis.get(key));
								}
									}	
								}
				
		    				}	
	               if(wert.isEmpty()){
	       			Set<String> keySet2 = this.analysis.keySet();
	   			    Iterator<String> i2 = keySet2.iterator();
	       			
	       			while(i2.hasNext()){	
	   					
	   					String key = i2.next();
	   					String neucheck = key.substring(0,1);
	   				
	   				
	   					if(lastletter.equals(neucheck)){
	   						
	   						
	   							//Werte im arry abspeichern
	   					//	System.out.println("empty");
	   						letter.add(key.substring(1,2));
	   						wert.add(analysis.get(key));
	   						
	   							}	
	   						}
	   		
	       				}	


			    }
			}
		    
			sixlastletter=fifthlastletter;
			fifthlastletter=fourthlastletter;	
			fourthlastletter=thirdlastletter;
			thirdlastletter=secondlastletter;
		    secondlastletter=lastletter;
		    
			// check the array lists
			List<Integer> wert1 = new ArrayList<Integer>();	
		
			int add=0;
			for(int s=0; s<wert.size(); s++){
				
				add=add+wert.get(s);					
				}
			
			// jedem wert einen prozentualen wert liefern
			
			for(int z=0; z<wert.size(); z++){
				int check;
				check=(wert.get(z)*100/add);
				wert1.add(check);
			}
			
			//zufahlszahl generieren
			
			int zufallszahl= (int) (Math.random()*100);
			int aufzaehlen=0;
			
			for(int t=0; t<wert.size();t++){
				
				aufzaehlen=aufzaehlen+wert1.get(t);
				if (zufallszahl<=aufzaehlen){
				nextletter=letter.get(t);
				text=text+nextletter;
				
				lastletter=nextletter;	
				break;
				}
			}
			
			
			
			
			
					
			if((ti+1)==wertlang){
				
				createGui2();
				
				this.setVisible(true);
				}  
				
			
		}
	
	textfield2.setText(text);
	textfield1.setText(textlaenge);
	


	}

	
	
	
}

